package main

import (
	"flag"
	"log"
	"net/http"
	"net/http/httputil"
	"net/url"
)

type ProxyServer struct {
	targetURL string
	proxy     *httputil.ReverseProxy
}

func (p *ProxyServer) handleRequest(w http.ResponseWriter, r *http.Request) {
	p.proxy.ServeHTTP(w, r)
}
func NewProxyServer(targetURL string) *ProxyServer {
	target, err := url.Parse(targetURL)
	if err != nil {
		log.Fatal("Error parsing target URL:", err)
	}

	proxy := httputil.NewSingleHostReverseProxy(target)

	return &ProxyServer{
		targetURL: targetURL,
		proxy:     proxy,
	}
}

func main() {
	targetURL := flag.String("target", "", "Target URL to proxy to")
	listenAddr := flag.String("addr", ":8080", "Address to listen on")

	if *targetURL == "" {
		log.Fatal("No Target URL specified")
	}

	proxy := NewProxyServer(*targetURL)
	log.Println("Proxying requests to:", *targetURL)
	log.Println("Listening on:", *listenAddr)

	http.HandleFunc("/", proxy.handleRequest)
	log.Fatal(http.ListenAndServe(*listenAddr, nil))
}
